package com.jingdianjichi.subject.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.jingdianjichi.subject.common.enums.CategoryTypeEnum;
import com.jingdianjichi.subject.common.enums.IsDeleteEnum;
import com.jingdianjichi.subject.convert.SubjectLabelConverter;
import com.jingdianjichi.subject.entity.SubjectLabelBO;
import com.jingdianjichi.subject.infra.basic.entity.SubjectCategory;
import com.jingdianjichi.subject.infra.basic.entity.SubjectLabel;
import com.jingdianjichi.subject.infra.basic.entity.SubjectMapping;
import com.jingdianjichi.subject.infra.basic.service.SubjectCategoryService;
import com.jingdianjichi.subject.infra.basic.service.SubjectLabelService;
import com.jingdianjichi.subject.infra.basic.service.SubjectMappingService;
import com.jingdianjichi.subject.infra.basic.service.impl.SubjectMappingServiceImpl;
import com.jingdianjichi.subject.service.SubjectLabelDomainService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 题目标签表(SubjectLabel)表服务实现类
 *
 * @author makejava
 * @since 2025-07-30 09:20:01
 */
@Service("subjectLabelDomainService")
public class SubjectLabelDomainServiceImpl implements SubjectLabelDomainService {
    @Resource
    private SubjectLabelService subjectLabelService;
    @Resource
    private SubjectMappingService subjectMappingService;
    @Resource
    private SubjectCategoryService subjectCategoryService;
    /**
     * 新增数据
     *
     * @param subjectLabelBO 实例对象
     * @return 实例对象
     */
    @Override
    public boolean add(SubjectLabelBO subjectLabelBO) {
        SubjectLabel subjectLabel = SubjectLabelConverter.Instatnce.convertBOToEntity(subjectLabelBO);
        subjectLabel.setIsDeleted(IsDeleteEnum.UN_DELETED.getCode());
        int count=this.subjectLabelService.insert(subjectLabel);
        return count>0;
    }

    @Override
    public Boolean update(SubjectLabelBO subjectLabelBO) {
        SubjectLabel subjectLabel = SubjectLabelConverter.Instatnce.convertBOToEntity(subjectLabelBO);
        subjectLabel.setIsDeleted(1);
        return subjectLabelService.update(subjectLabel)>0;
    }

    @Override
    public Boolean delete(SubjectLabelBO subjectLabelBO) {
        SubjectLabel subjectLabel = SubjectLabelConverter.Instatnce.convertBOToEntity(subjectLabelBO);
        subjectLabel.setIsDeleted(1);
        return subjectLabelService.update(subjectLabel)>0;
    }

    @Override
    public List<SubjectLabelBO> queryLabelByCategoryId(SubjectLabelBO subjectLabelBO) {

        //如果当前分类是1级分类，则查询所有标签
        SubjectCategory subjectCategory = subjectCategoryService.queryById(subjectLabelBO.getCategoryId());
        if(CategoryTypeEnum.PRIMARY.getCode() == subjectCategory.getCategoryType()){
            SubjectLabel subjectLabel = SubjectLabelConverter.Instatnce.convertBOToEntity(subjectLabelBO);
            subjectLabel.setIsDeleted(IsDeleteEnum.UN_DELETED.getCode());
            List<SubjectLabel> subjectLabels = subjectLabelService.queryLabelByCategoryId(subjectLabel);
            return SubjectLabelConverter.Instatnce.convertEntityToBO(subjectLabels);
        }

        Long categoryId = subjectLabelBO.getCategoryId();
        SubjectMapping subjectMapping = new SubjectMapping();
        subjectMapping.setCategoryId(categoryId);
        subjectMapping.setIsDeleted(IsDeleteEnum.UN_DELETED.getCode());
        List<SubjectMapping> mappingList = subjectMappingService.queryLabelId(subjectMapping);
        if (CollectionUtils.isEmpty(mappingList)) {
            return Collections.emptyList();
        }
        List<Long> labelIdList = mappingList.stream().map(SubjectMapping::getLabelId).collect(Collectors.toList());
        List<SubjectLabel> labelList = subjectLabelService.batchQueryById(labelIdList);
        List<SubjectLabelBO> boList = new LinkedList<>();
        labelList.forEach(label -> {
            SubjectLabelBO bo = new SubjectLabelBO();
            bo.setId(label.getId());
            bo.setLabelName(label.getLabelName());
            bo.setCategoryId(categoryId);
            bo.setSortNum(label.getSortNum());
            boList.add(bo);
        });
        return boList;

    }

}
